package cn.jinjimi.app.modules.notify;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import cn.jinjimi.framework.helper.annotation.EventTrace;
import cn.jinjimi.framework.helper.annotation.EventTraceParam;
import cn.jinjimi.app.AccountManager;
import cn.jinjimi.app.bean.EventDataField;
import cn.jinjimi.app.bean.EventID;
import cn.jinjimi.app.bean.BundleKey;
import cn.jinjimi.app.managers.LocalPushActInfoManager;
import cn.jinjimi.app.modules.notify.notifications.NotificationBuilder;
import cn.jinjimi.app.modules.notify.notifications.PushDTO;

/**
 * Created by Roy
 * Date: 16/10/11
 */

public class LocalReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        if (AccountManager.getInstance().isLogin()) {

            int code = intent.getIntExtra(BundleKey.CODE, 0);

            String title = intent.getStringExtra(BundleKey.TITLE);
            String url = intent.getStringExtra(BundleKey.URL);
            String content = intent.getStringExtra(BundleKey.CONTENT);

            PushDTO notification = new PushDTO();
            notification.url = url;
            notification.title = title;
            notification.alert = content;

            PendingIntent pendingIntent = NotificationDispatcher.getPendingIntent(context,
                    notification, NotificationDispatcher.getRequestCode(), PendingIntent.FLAG_ONE_SHOT);

            NotificationBuilder builder = new NotificationBuilder(context)
                    .alert(content)
                    .title(title)
                    .code(code)
                    .pending(pendingIntent);

            NotificationDispatcher.sendNotification(context, builder);
            NotificationDispatcher.playRingtone(context);

            LocalPushActInfoManager.PushAllSet(context);

            // 2.6.1添加，本地推送添加与远程推送同样的事件统计 from为空，id为0，type为0 2017/2/13 Roy
            analyse(String.valueOf(notification.getFromSorce()), String.valueOf(notification.getType()),
                    notification.getUrl(), notification.getSource());
        }
    }

    @EventTrace(EventID.APP_PUSH_REACHED)
    private void analyse(@EventTraceParam(EventDataField.ParamId) String id, @EventTraceParam(EventDataField.ParamType) String type,
                         @EventTraceParam(EventDataField.ParamTo) String to, @EventTraceParam(EventDataField.ParamFrom) String from) {
    }
}
